package a11_单调栈;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * <p>
 * a02_下一个更大元素1复习1
 * </p>
 *
 * @author flyduck
 * @since 2025/3/7
 */
public class a02_下一个更大元素1复习1 {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] result = new int[nums1.length];
        Arrays.fill(result, -1);
        //nums1数据 -> nums1索引
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            map.put(nums1[i],i);
        }

        Stack<Integer> stack = new Stack<>();
        stack.push(nums2[0]);

        for (int i = 1; i < nums2.length; i++) {
            while (!stack.isEmpty() && stack.peek() < nums2[i]){
                Integer pop = stack.pop();
                if(map.containsKey(pop)){
                    result[map.get(pop)] = nums2[i];
                }
            }
            stack.push(nums2[i]);
        }

        return result;
    }
}
